package com.hanson.controller;

import com.hanson.common.bean.ResultMessage;
import com.hanson.entity.CartBean;
import com.hanson.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/web/cart")
public class WebCartController {
    @Autowired
    private CartService cartService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(@RequestBody CartBean cartBean, @CookieValue("openId") String openId) throws Exception {
        cartService.addCart(openId, cartBean);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("加入购物车成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultMessage modify(@RequestBody CartBean cartBean, @CookieValue("openId") String openId) throws Exception {
        cartService.updateCart(openId, cartBean);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("修改成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResultMessage delete(@RequestBody String[] ids) throws Exception {
        cartService.deleteCart(ids);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("删除地址成功！");
        return resultMessage;
    }
}
